package com.paifori.cloud.security.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.paifori.cloud.security.annotation.ExcelColumn;
import com.paifori.cloud.security.bean.User;
import com.paifori.cloud.security.utils.excel.ExcelUtils;

import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;

/**
 * @project
 * @Description
 * @Author songwp
 * @Date 2022/8/24 16:11
 * @Version 1.0.0
 **/
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @GetMapping(value = "/template")
    public void template(@RequestParam(defaultValue = ExcelColumn.CH) String lang, HttpServletResponse response)
	    throws IOException {

	var headers = List.of(header(User.class, "userCode", lang), header(User.class, "userName", lang),
		header(User.class, "dept", lang), header(User.class, "userStatus", lang));

	ExcelUtils.writeExcelTemplate(response, headers, "template.xlsx");
    }

    private String header(Class<?> clazz, String field, String lang) {
	try {
	    var field1 = clazz.getDeclaredField(field);
	    if (null != field1) {
		ExcelColumn cloumn = field1.getAnnotation(ExcelColumn.class);
		if (null != cloumn) {
		    return ExcelColumn.CH.equals(lang) ? cloumn.value() : cloumn.valueEn();
		}
	    }
	} catch (NoSuchFieldException e) {
	    e.printStackTrace();
	} catch (SecurityException e) {
	    e.printStackTrace();
	}
	return "";
    }

    @GetMapping(value = "/export")
    public void exportExcel(@RequestParam(defaultValue = ExcelColumn.CH) String lang, HttpServletResponse response)
	    throws IOException {
	var resultList = new ArrayList<User>();
	var user = new User();
	user.setUserCode("S0001");
	user.setUserName("张三");
	user.setUserStatus(0);
	user.setDept("研发部");
	resultList.add(user);

	user = new User();
	user.setUserCode("S0002");
	user.setUserName("李四");
	user.setUserStatus(0);
	user.setDept("研发部");
	resultList.add(user);
	var str = STR."My name is \{user.getUserName()}";
		log.info("", resultList);
	ExcelUtils.writeExcel(response, resultList, User.class, lang);
    }

    @PostMapping(value = "/import")
    public void readExcel(@RequestParam(defaultValue = ExcelColumn.CH) String lang,
	    @RequestParam(value = "upload_file", required = false) MultipartFile file) {
	var list = ExcelUtils.readExcel("", User.class, file, lang);
	var mapper = new ObjectMapper();
	list.forEach(b -> {
	    try {
		System.out.println(mapper.writeValueAsString(b));
	    } catch (JsonProcessingException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	    }
	});
    }
}